﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace GGWP
{
    class CountANDSize
    {
        public int _count;
        public long _size;

        public CountANDSize()
        {
            _count = 0;
            _size = 0;
        }

    }
    class Fileee
        {
            public string Name { get; set; }
            public long Size { get; set; }
        }
    class Simple
    {
        private List<Fileee> fileee;
        private List<string> extension;
        private List<string> log;

        public Simple()
        {
            this.fileee    = new List<Fileee>();
            this.extension = new List<String>();
            this.log       = new List<String>(); 
        }

        CountANDSize Count(List<Fileee> fileee, string extension)
        {
            CountANDSize co = new CountANDSize();
            foreach (Fileee fi in fileee)
                {
                    if (extension == fi.Name)
                    {
                        co._count += 1;
                        co._size += fi.Size;
                    }
                }
            return co;
        }

        bool IsConsist(List<string> Ext, string fExt)
        {
            int count = 0;
            foreach (string ext in Ext)
            {
                if (fExt == ext)
                    count += 1;
                else
                    count += 0;
            }
            if (count > 0)
                return true;
            else return false;
        }
        
        void GroupByEx1(FileInfo f)
        {
            fileee.Add(new Fileee() { Name = f.Extension, Size = f.Length });
            if (extension.Count == 0)
                extension.Add(f.Extension);

            if (IsConsist(extension, f.Extension))
            {
                { }
            }

            else
            {
                extension.Add(f.Extension);
            }
        }

        void Watch()
        {
            IEnumerable<IGrouping<string, long>> query = fileee.GroupBy(file => file.Name, file => file.Size);

            foreach (IGrouping<string, long> fileGroup in query)
            {
                Console.Write(fileGroup.Key);
                Console.WriteLine("  количество файлов: {0}. Занимаемый файлами объем: {1}мб", Count(fileee, fileGroup.Key)._count, Count(fileee, fileGroup.Key)._size/1024/1024);
            }
        }
        void DisplayFiles()
        {
            DriveInfo[] drive = DriveInfo.GetDrives();

            foreach (DriveInfo d in drive)
            {
                DirectoryInfo dir = new DirectoryInfo(d.Name);
                if (d.IsReady)
                CheckFiles(dir);
                Watch();
                Console.WriteLine("Press any key... ");
                Console.ReadKey();
                fileee.Clear();
            }
        }

        void CheckFiles(DirectoryInfo dir)
        {
            FileInfo[] files = null;
            DirectoryInfo[] subDirs = null;

            try
            {
                files = dir.GetFiles("*.*");
            }

            catch (UnauthorizedAccessException e)
            {
                log.Add(e.Message);
            }

            if (files != null)
            {
                foreach (FileInfo f in files)
                {
                    GroupByEx1(f);
                }

                subDirs = dir.GetDirectories();

                foreach (DirectoryInfo dirf in subDirs)
                {
                    CheckFiles(dirf);
                }
            }
        } 
        
        static void Main()
        {
            
            Simple sim = new Simple();
            sim.DisplayFiles();
            Console.ReadLine();
        }
    }
}





